<?php
namespace Tbf\Bundle\PackageBundle\Type;

class GroupManager
{
    const INCLUDE_POLICY = 'include_policy';
    const EXCLUDE_POLICY = 'exclude_policy';
    protected $exclude_list = array();
    protected $include_list = array();
    // 对不在列表里面的项的处理策略
    protected $not_exist_policy = GroupManager::INCLUDE_POLICY;
    public function excludeGroup($group)
    {
        $this->exclude_list[$group] = 1;
        unset($this->include_list[$group]);

        return $this;
    }
    public function includeGroup($group)
    {
        $this->include_list[$group] = 1;
        unset($this->exclude_list[$group]);

        return $this;
    }
    public function includeGroupList($group_list)
    {
        foreach ($group_list as $group) {
            $this->includeGroup($group);
        }

        return $this;
    }
    public function excludeGroupList($group_list)
    {
        foreach ($group_list as $group) {
            $this->excludeGroup($group);
        }

        return $this;
    }
    public function reset()
    {
        $this->include_list = array();
        $this->exclude_list = array();
        $this->not_exist_policy = GroupManager::INCLUDE_POLICY;

        return $this;
    }

    /**
     * 是否和某个组匹配
     * @param $group
     */
    public function match($group)
    {
        if (!empty($this->exclude_list[$group])) {
            return false;
        }
        if (!empty($this->include_list[$group])) {
            return true;
        }
        if ($this->not_exist_policy===GroupManager::INCLUDE_POLICY) {
            return true;
        } else {
            return false;
        }
    }
    public function includeNotExistGroup()
    {
        $this->not_exist_policy = GroupManager::INCLUDE_POLICY;

        return $this;
    }

    public function excludeNotExistGroup()
    {
        $this->not_exist_policy = GroupManager::EXCLUDE_POLICY;

        return $this;
    }

    /**
     * @return GroupManager
     */
    public static function create()
    {
        return new static();
    }
}
